/* $Id:server.h jjs $ */
/*
   Copyright (C) 2007 by Jurjen Stellingwerff <jurjen@stwerff.xs4all.nl>
   Part of the Moros Project http://moros.sourceforge.net/

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License.
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY.

   See the COPYING file for more details.
*/
#include <string>
#include <map>
#include "../general/communicate.h"

#define state_editor 1
#define state_game 2
#define state_pauzed 3
#define state_winner 4
#define state_finished 5

class server {
  private:
    int join; ///< join rule
    int goal; ///< game goal
    unsigned long c_time;
    unsigned int turns:10;   ///< number of turns before end
    unsigned int percentage:7; ///< percentage of total points is winning
    unsigned int speed;   ///< number of seconds for each turn
    unsigned int no_player:10;	///< maximum number of players for each party
    std::string central_server; ///< Ip address of the central server
    communicate comm; ///< Client connection
    channel *c_channel; ///< current communication channel
    communicate cent_comm; ///< Central server connection
    channel *cent_ch; ///< Communication channel to the central server to check session id's
    std::map<std::string, sigc::signal<void, std::string> > commands; ///< Commands and the handler
    void data_handler(channel *ch); ///< Read the data of the game
    void client_handler(channel *ch); ///< routines to handle communication with the central server
    void handler(channel *ch); ///< routine to handle messages and commands from players
    int state; ///< Current state of the game
    void data_session(); ///< Create a normal session to retreive game data from the central server
    void send_tile(pos t); ///< Send terrain data and cities
    string export_file;
    std::list<std::string> import_files;

  public:
    server();
    void set_import(std::string);
    void set_export(std::string);
    void move(std::string);
    void set_add(std::string type);
    void set_number(std::string nr);
    void get_refresh(std::string pos);
    void ip_addr(std::string host); ///< set the ip address of the central server
    void session_set(std::string session); ///< set the session id with the central server
    void log_level(std::string level);
    void log_file(std::string logfile);
    void do_fork(std::string file); ///< fork this task to the background
    void test_map(std::string file); ///< Read a test map for developing the editor
    void main_loop(); ///< main connunication loop
};
